package com.zhsq.zlc.controller.supplier;

import com.zhsq.zlc.entity.IDEntity;
import com.zhsq.zlc.entity.LogEntity;
import com.zhsq.zlc.entity.ResultObject;
import com.zhsq.zlc.entity.SupplierEntity;
import com.zhsq.zlc.service.log.LogService;
import com.zhsq.zlc.service.supplier.SupplierService;
import com.zhsq.zlc.util.Constant;
import com.zhsq.zlc.util.PageUtil;
import com.zhsq.zlc.util.SessionInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    @Autowired
    private LogService logService;


    @RequestMapping("zhsq/supplierManage_{pageCurrent}_{pageSize}_{pageCount}")
    public String supplierManage(SupplierEntity entity, @PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @PathVariable Integer pageCount, Model model) throws Exception {
        //判断
        if (pageSize == 0) pageSize = 10;
        if (pageCurrent == 0) pageCurrent = 1;
        int rows = supplierService.count(entity);
        if (pageCount == 0) pageCount = rows % pageSize == 0 ? (rows / pageSize) : (rows / pageSize) + 1;

        //查询
        entity.setStart((pageCurrent - 1) * pageSize);
        entity.setEnd(pageSize);
        List<SupplierEntity> list = supplierService.list(entity);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setDataIndex(entity.getStart() + i + 1);
            }
        }
        //输出
        model.addAttribute("list", list);
        String pageHTML = PageUtil.getPageContent("supplierManage_{pageCurrent}_{pageSize}_{pageCount}?supplierName=" + entity.getSupplierName(), pageCurrent, pageSize, pageCount);
        model.addAttribute("pageHTML", pageHTML);
        model.addAttribute("supplier", entity);
//        System.out.println(list);
        return "zhsq/supplierManage";
    }


    /**
     * 字典新增、修改跳转
     * @param model
     * @param entity
     * @return
     */
    @PostMapping("/zhsq/supplierEditGetEntity")
    public String supplierEditGetEntity(Model model,SupplierEntity entity) throws Exception{
        if(entity.getId()!=null){
            model.addAttribute("supplier",supplierService.getSupplierById(entity));
        }else{
            model.addAttribute("supplier",new SupplierEntity());
        }
        return "zhsq/supplierEdit";
    }

    /**
     * 字典新增、修改提交
     * @param model
     * @param entity
     * @param httpSession
     * @return
     */
   @PostMapping("/zhsq/supplierEdit")
    public String supplierEditPost(Model model, SupplierEntity entity, HttpSession httpSession, HttpServletRequest request)throws Exception {
        HttpSession session = request.getSession();
        SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
        if(entity.getId()!=null){
            supplierService.edit(entity);
            this.logService.insertLog(LogEntity.EDIT_TYPE, "修改供应商数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
        } else {
            supplierService.add(entity);
            this.logService.insertLog(LogEntity.INSERT_TYPE, "添加供应商数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
        }
        return "redirect:supplierManage_0_0_0";
    }

    /**
     * 删除字典
     * @param entity
     * @return
     */
    @ResponseBody
    @PostMapping("/zhsq/supplierDelete")
    public ResultObject<Object> supplierDelete(IDEntity entity, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
        supplierService.delete(entity.getIntID());
        this.logService.insertLog(LogEntity.DELETE_TYPE, "删除用户数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
        ResultObject<Object> object = new ResultObject<Object>(Constant.Code01, Constant.Msg01,null, null);
        return object;
    }
}
